package hlhj.fhp.newslib.activitys

import android.content.Intent
import android.support.v7.app.AppCompatActivity
import android.os.Bundle
import android.support.design.widget.TabLayout
import android.support.v4.app.Fragment
import android.support.v7.widget.LinearLayoutManager
import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.view.WindowManager
import android.widget.TableLayout
import com.bumptech.glide.Glide
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.BaseViewHolder
import com.lzy.okgo.model.Response
import com.tenma.ventures.bean.utils.TMSharedPUtil
import hlhj.fhp.newslib.BaseAty
import hlhj.fhp.newslib.R
import hlhj.fhp.newslib.adapters.FragmentAdp
import hlhj.fhp.newslib.customview.GoLoginDialog
import hlhj.fhp.newslib.fragments.RecommendFgm
import hlhj.fhp.newslib.javabean.ColumBean
import hlhj.fhp.newslib.javabean.LogoBean
import hlhj.fhp.newslib.javabean.TabBean
import hlhj.fhp.newslib.network.Apis
import hlhj.fhp.newslib.network.CustomAction
import hlhj.fhp.newslib.network.LoadingAction
import hlhj.fhp.newslib.utils.GlideUtil
import hlhj.fhp.newslib.utils.ToastUtil
import kotlinx.android.synthetic.main.activity_main_aty.*
import kotlinx.android.synthetic.main.home_top_item.view.*
import kotlinx.android.synthetic.main.recommend_article.*
import java.util.ArrayList

class MainAty : BaseAty() {
    private val tabStrs=ArrayList<String>()
    private val fgmList=ArrayList<Fragment>()
    private lateinit var fgmPagerAdp:FragmentAdp
    private lateinit var recommendFgm:Fragment
    private val tabs=ArrayList<TabBean>()
    override fun getContentId(): Int = R.layout.activity_main_aty
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        ToastUtil.init(applicationContext)
    }
    override fun initListener() {
        btMore.setOnClickListener {
            if (TMSharedPUtil.getTMToken(this).isEmpty()){
                GoLoginDialog(this).show()
                return@setOnClickListener
            }
            val intent = Intent(this, ChooseAty::class.java)
            startActivity(intent)
        }
        btGoSearch.setOnClickListener {
            val intent = Intent(this, SearchAty::class.java)
            startActivity(intent)
        }
    }

    override fun initView() {
        viewPager.offscreenPageLimit=10
        tabLayout.tabMode=TabLayout.MODE_SCROLLABLE
        disPatchColumn()

    }

    fun disPatchColumn(){
        /*列表*/
        Apis.getColumnList(TMSharedPUtil.getTMUser(this).member_id)
                .doOnSubscribe(LoadingAction(this))
                .subscribe(object :CustomAction<Response<ColumBean>>{
                    override fun onResult(t: Response<ColumBean>) {
                        loadingDialog.dismiss()
                        val columBean = t.body()
                        for (i in 0..columBean.data.defaultX.size-1){
                            val tabBean = TabBean()
                            tabBean.id=columBean.data.defaultX[i].id
                            tabBean.name=columBean.data.defaultX[i].category_name
                            tabs.add(tabBean)
                        }
                        initTab()
                    }
                })
        /*logo*/
        Apis.getLogo()
                .doOnSubscribe{

                }.subscribe(object :CustomAction<Response<LogoBean>>{
            override fun onResult(t: Response<LogoBean>) {
                val logoBean = t.body()
                Glide.with(this@MainAty).load(logoBean.data).apply(GlideUtil.loadIMGOption()).into(imgLogo)

            }
        })
    }

    private fun getTab(i:Int): View? {
        val view = LayoutInflater.from(this).inflate(R.layout.home_top_item, null)
        view.itemTittle.text=tabStrs.get(i)
        return view
    }

    private fun initTab(){
        for (i in 0..tabs.size-1){
            val tab = tabLayout.newTab()
            tab.setText(tabs[i].name)
            tabLayout.addTab(tab)
            val instence = RecommendFgm.getInstence(tabs[i].id,tabs[i].name)
            fgmList.add(instence)
        }

        fgmPagerAdp=FragmentAdp(supportFragmentManager,fgmList)
        viewPager.adapter=fgmPagerAdp
        tabLayout.setupWithViewPager(viewPager)
    }
}
